package com.coursebooking.repository;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.coursebooking.model.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.Optional;

@Mapper
public interface UserRepository extends BaseMapper<User> {
    
    @Select("SELECT * FROM user WHERE username = #{username}")
    User selectByUsername(String username);
    
    @Select("SELECT * FROM user WHERE email = #{email}")
    User selectByEmail(String email);
    
    @Select("SELECT COUNT(*) FROM user WHERE username = #{username}")
    int countByUsername(String username);
    
    @Select("SELECT COUNT(*) FROM user WHERE email = #{email}")
    int countByEmail(String email);
    
    // 为了保持兼容性，添加包装方法
    default User findByUsername(String username) {
        return selectByUsername(username);
    }
    
    default User findByEmail(String email) {
        return selectByEmail(email);
    }
    
    default boolean existsByUsername(String username) {
        return countByUsername(username) > 0;
    }
    
    default boolean existsByEmail(String email) {
        return countByEmail(email) > 0;
    }
}